package ncov.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import ncov.model.Student;
import ncov.service.StudentService;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author WuChangJian
 * @date 2020/6/15 19:38
 */
public class FrontInterceptor extends MethodFilterInterceptor {
    private static final String REPORT_SUCCESS = "report_success";
    private static final String FRONT_LOGIN = "front_login";
    @Autowired
    private StudentService studentService;

    @Override
    protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        Object obj = session.getAttribute("student");
        if (obj != null) {
            Student stu = (Student) obj;
            Student temp = studentService.getStudent(stu.getStuId());
            if (temp.getReportRecord() != null) {
                // 说明今日已经上报，防止重复上报直接返回成功页面
                return REPORT_SUCCESS;
            } else {
                return actionInvocation.invoke();
            }
        } else {
            return FRONT_LOGIN;
        }
    }
}
